﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using FoosballEye.Model;

namespace FoosballEye.Repository
{
    using FoosballEye.Model.Helper;

    public class LoginHistoryRepository : RepositoryBase<FoosballEyeDbContext>, ILoginHistoryRepository
    {
        readonly bool _localDataOnly = Boolean.Parse(ConfigurationManager.AppSettings["LocalDataOnly"]);

        public OperationStatus InsertLoginHistory(string login)
        {
            var loginHistory = new LoginHistory { Login = login, InsertDate = DateTime.Now };

            var opStatus = new OperationStatus { Status = true, OperationObject = loginHistory};
            if (_localDataOnly) return opStatus;

            try
            {
                DataContext.LoginHistories.Add(loginHistory);
                DataContext.SaveChanges();
            }
            catch (Exception exp)
            {
                opStatus = OperationStatus.CreateFromException("Error inserting history of logining.", exp, loginHistory);
            }
            return opStatus;
        }
    }
}
